package com.jiumi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jiumi.baseconfig.webapi.AliPayController;
import com.jiumi.common.annotation.DataScope;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.StringUtils;
import com.jiumi.order.domain.BaseOrderDetail;
import com.jiumi.order.domain.BaseOrderInfo;
import com.jiumi.order.service.IBaseOrderDetailService;
import com.jiumi.order.service.IBaseOrderInfoService;
import org.apache.poi.util.StringUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jiumi.common.annotation.Log;
import com.jiumi.common.core.controller.BaseController;
import com.jiumi.common.core.domain.AjaxResult;
import com.jiumi.common.enums.BusinessType;
import com.jiumi.order.domain.BaseOrderPayInfo;
import com.jiumi.order.service.IBaseOrderPayInfoService;
import com.jiumi.common.utils.poi.ExcelUtil;
import com.jiumi.common.core.page.TableDataInfo;

/**
 * 收款管理Controller
 *
 * @author jiumi
 * @date 2023-06-02
 */
@RestController
@RequestMapping("/order/BaseOrderPayInfo")
public class BaseOrderPayInfoController extends BaseController {
    @Autowired
    private IBaseOrderPayInfoService baseOrderPayInfoService;

    @Autowired
    private IBaseOrderInfoService baseOrderInfoService;

    @Autowired
    private IBaseOrderDetailService baseOrderDetailService;


    @Autowired
    private AliPayController aliPayController;
    /**
     * 查询收款管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:list')")
    @GetMapping("/list")
    @DataScope(userAlias = "u")
    public TableDataInfo list(BaseOrderPayInfo baseOrderPayInfo) {
        startPage();
        List<BaseOrderPayInfo> list = baseOrderPayInfoService.selectBaseOrderPayInfoList(baseOrderPayInfo);
        return getDataTable(list);
    }

    /**
     * 导出收款管理列表
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:export')")
    @Log(title = "收款管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseOrderPayInfo baseOrderPayInfo) {
        List<BaseOrderPayInfo> list = baseOrderPayInfoService.selectBaseOrderPayInfoList(baseOrderPayInfo);
        ExcelUtil<BaseOrderPayInfo> util = new ExcelUtil<BaseOrderPayInfo>(BaseOrderPayInfo.class);
        util.exportExcel(response, list, "收款管理数据");
    }

    /**
     * 获取收款管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(baseOrderPayInfoService.selectBaseOrderPayInfoById(id));
    }

    /**
     * 新增收款管理
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:add')")
    @Log(title = "收款管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseOrderPayInfo baseOrderPayInfo) {

        Long orderId = baseOrderPayInfo.getOrderId();
        if(StringUtils.isEmpty(baseOrderPayInfo.getPayment())){
            return AjaxResult.error("收款方式不能为空");
        }
        if (StringUtils.isNull(orderId)) {
            return AjaxResult.error("订单ID不能为空，操作异常");
        }
        if (StringUtils.isNull(baseOrderPayInfo.getOrderDetailId())) {
            return AjaxResult.error("订单明细不能为空，操作异常");
        }
        BaseOrderInfo baseOrderInfo = baseOrderInfoService.selectBaseOrderInfoById(orderId);
        if (null == baseOrderInfo) {
            return AjaxResult.error("订单未找到");
        }
        List<BaseOrderDetail> checkList= baseOrderDetailService.selectOrderDetailListById(baseOrderPayInfo.getOrderDetailId());
        if(checkList.size()==0){
            return AjaxResult.error("参数错误");
        }
        baseOrderPayInfo.setOrderNumber(baseOrderInfo.getOrderNumber());
        baseOrderPayInfo.setUserId(baseOrderInfo.getUserId());
        baseOrderPayInfo.setUserName(baseOrderInfo.getUserName());
        baseOrderPayInfo.setUserPhone(baseOrderInfo.getPhonenumber());
        baseOrderPayInfo.setPaymentStatus("0");
        baseOrderPayInfo.setCreateTime(DateUtils.getNowDate());
        baseOrderPayInfo.setCreateBy(String.valueOf(getUserId()));
        baseOrderPayInfo.setOrderAmount(baseOrderInfo.getOrderAmount());
        baseOrderPayInfo.setRealAmount(baseOrderInfo.getRealAmount());
        baseOrderPayInfo.setPayOrderNumber(StringUtils.getRandomStr());
        int resultNum= baseOrderPayInfoService.insertBaseOrderPayInfo(baseOrderPayInfo);
        if(resultNum>0){

            BaseOrderDetail detailParam=new BaseOrderDetail();
            detailParam.setPayIdStr(baseOrderPayInfo.getOrderDetailId());
            detailParam.setPayId(baseOrderPayInfo.getId());
            baseOrderDetailService.updateBaseOrderDetailPid(detailParam);
            if("支付宝".equals(baseOrderPayInfo.getPayment())) {
                aliPayController.pcPay(baseOrderPayInfo);
            }
        }
        return toAjax(resultNum);
    }

    /**
     * 修改收款管理
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:edit')")
    @Log(title = "收款管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseOrderPayInfo baseOrderPayInfo) {
        return toAjax(baseOrderPayInfoService.updateBaseOrderPayInfo(baseOrderPayInfo));
    }

    /**
     * 删除收款管理
     */
    @PreAuthorize("@ss.hasPermi('order:BaseOrderPayInfo:remove')")
    @Log(title = "收款管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        BaseOrderPayInfo payInfo= baseOrderPayInfoService.selectBaseOrderPayInfoById(id);
        if("1".equals(payInfo.getPaymentStatus())){
            return AjaxResult.error("已付款订单不能删除");
        }
        return toAjax(baseOrderPayInfoService.deleteBaseOrderPayInfoById(id));
    }

    @Log(title = "取消订单管理", businessType = BusinessType.UPDATE)
    @GetMapping("/cancelPayOrderInfo/{id}")
    public AjaxResult cancelPayOrderInfo(@PathVariable Long id) {
        BaseOrderPayInfo payInfo= baseOrderPayInfoService.selectBaseOrderPayInfoById(id);
        if(!"1".equals(payInfo.getPaymentStatus())){
            return AjaxResult.error("只有已支付的订单才能进行取消");
        }
        payInfo.setUpdateBy(getUsername());
        return toAjax(baseOrderPayInfoService.cancelBaseOrderPayInfoById(payInfo));
    }
}
